package com.fuint.utils.syb;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.fuint.utils.syb.SybConstants.PAY_TYPE_WECHAT;
import static com.fuint.utils.syb.SybUtil.parseEntity;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SybWxPayDTO implements SybBaseDTO {
    /**
     * 集团/代理商商户号（共享集团号/代理商参数时必填）
     */
    @JsonAlias("orgid")
    private String orgId;
    /**
     * 实际交易的商户号
     */
    @JsonAlias("cusid")
    private String cusId;
    /**
     * 平台分配的APPID
     */
    private String appid;
    /**
     * 版本号（默认11）
     */
    private String version;
    /**
     * 交易金额（单位为分）
     */
    @JsonAlias("trxamt")
    private String trxAmt;

    @JsonAlias("reqsn")
    private String reqSn;

    @JsonAlias("unireqsn")
    private String uniReqSn;

    @JsonAlias("paytype")
    private String payType;

    @JsonAlias("randomstr")
    private String randomStr;

    private String body;

    private String remark;

    @JsonAlias("validtime")
    private String validTime;

    @JsonAlias("expiretime")
    private String expireTime;

    private String acct;

    @JsonAlias("notify_url")
    private String notifyUrl;

    @JsonAlias("limit_pay")
    private String limitPay;

    @JsonAlias("sub_appid")
    private String subAppid;

    @JsonAlias("goods_tag")
    private String goodsTag;

    @JsonAlias("benefitdetail")
    private String benefitDetail;

    @JsonAlias("chnlstoreid")
    private String chnlStoreId;

    private String subbranch;

    @JsonAlias("extendparams")
    private String extendParams;

    @JsonAlias("cusip")
    private String cusIp;

    @JsonAlias("front_url")
    private String frontUrl;

    @JsonAlias("idno")
    private String idNo;

    @JsonAlias("truename")
    private String trueName;

    @JsonAlias("asinfo")
    private String asInfo;

    @JsonAlias("fqnum")
    private String fqNum;

    @JsonAlias("signtype")
    private String signType;

    @JsonAlias("unpid")
    private String unPid;

    @JsonAlias("finorg")
    private String finOrg;

    private String sign;

    @JsonAlias("operatorid")
    private String operatorId;

    /**
     * 预支付初始化
     *
     * @param trxAmt           金额
     * @param sn               订单号
     * @param body             商品描述
     * @param remark           备注
     * @param acct             openId
     * @param appid            通联AppId
     * @param cusId            通联商户号
     * @param sybSm2PrivateKey 私钥
     * @param notifyUrl        回调地址
     */
    public void payInit(String orgId,
                        long trxAmt,
                        String sn,
                        String body,
                        String remark,
                        String acct,
                        String appid,
                        String cusId,
                        String sybSm2PrivateKey,
                        String notifyUrl) throws Exception {
        if (StrUtil.isNotEmpty(orgId)) {
            this.setOrgId(orgId);
        }
        this.setCusId(cusId);
        this.setAppid(appid);
        this.setVersion("11");
        this.setTrxAmt(String.valueOf(trxAmt));
        this.setReqSn(sn);
        this.setPayType(PAY_TYPE_WECHAT);
        this.setRandomStr(SybUtil.getValidatecode(8));
        this.setBody(body);
        this.setRemark(remark);
        this.setValidTime("5");
        this.setExpireTime(LocalDateTime.now().plusMinutes(5).format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        this.setAcct(acct);
        this.setNotifyUrl(notifyUrl);
        this.setLimitPay("no_credit");
        // 小程序APPID
        this.setSubAppid("wx01a890691c1eb0d0");
        this.setSignType(SybConstants.SIGN_TYPE);
        this.setSign(SybUtil.unionSign(parseEntity(this), sybSm2PrivateKey, SybConstants.SIGN_TYPE));
    }

    public void payCashierInit(String orgId,
                        long trxAmt,
                        String sn,
                        String body,
                        String remark,
                        String acct,
                        String appid,
                        String cusId,
                        String sybSm2PrivateKey,
                        String notifyUrl) throws Exception {
        this.setAppid(appid);
        this.setBody(body);
        this.setCusId(cusId);
        // 去掉该字段，用于支持信用卡支付
        // this.setLimitPay("no_credit");
        this.setNotifyUrl(notifyUrl);
        if (StrUtil.isNotEmpty(orgId)) {
            this.setOrgId(orgId);
        }
        this.setPayType(PAY_TYPE_WECHAT);
        this.setRandomStr(SybUtil.getValidatecode(8));
        this.setRemark(remark);
        this.setReqSn(sn);
        this.setSignType(SybConstants.SIGN_TYPE);
        this.setTrxAmt(String.valueOf(trxAmt));
        this.setValidTime("5");
        this.setVersion("12");
        this.setSign(SybUtil.unionSign(parseEntity(this), sybSm2PrivateKey, SybConstants.SIGN_TYPE));
    }

    public void payFullInit(long trxAmt, String sn, String body, String remark, String acct, String appid, String cusId, String sybSm2PrivateKey, String notifyUrl) throws Exception {
        String random = SybUtil.getValidatecode(8);
        this.setOrgId("");
        this.setCusId(cusId);
        this.setAppid(appid);
        this.setVersion("11");
        this.setTrxAmt(String.valueOf(trxAmt));
        this.setReqSn(sn);
        this.setUniReqSn("");
        this.setPayType(PAY_TYPE_WECHAT);
        this.setRandomStr(random);
        this.setBody(body);
        this.setRemark(remark);
        this.setValidTime("5");
        this.setExpireTime(LocalDateTime.now().plusMinutes(5).format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        this.setAcct(acct);
        this.setNotifyUrl(notifyUrl);
        this.setLimitPay("no_credit");
        // 小程序APPID
        this.setSubAppid("wx01a890691c1eb0d0");
        this.setGoodsTag("");
        this.setBenefitDetail("");
        this.setChnlStoreId("");
        this.setSubbranch("");
        this.setExtendParams("");
        this.setCusIp("");
        this.setFrontUrl("");
        this.setIdNo("");
        this.setTrueName("");
        this.setAsInfo("");
        this.setFqNum("");
        this.setSignType(SybConstants.SIGN_TYPE);
        this.setUnPid("");
        this.setFinOrg("");
        this.setOperatorId("");
        this.setSign(SybUtil.unionSign(parseEntity(this), sybSm2PrivateKey, SybConstants.SIGN_TYPE));
    }

    public void refundInit() {
    }

    public void queryInit() {
    }

    public void cancelInit() {
    }
}
